package com.aier.user.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.common.common.Constant;
import com.aier.user.entity.Follow;

public class FollowRowMapper implements RowMapper<Follow> {

    @SuppressWarnings("unused")
    private int type;
    
    public static final int FOLLOW_BRIEF=1;
    public static final int FOLLOW_DO=2;
    public static final int FOLLOW_FRIEND = 3;

    public FollowRowMapper() {
    }

    public FollowRowMapper(int type) {
        this.type = type;
    }

    @Override
    public Follow mapRow(ResultSet rs, int rowNum) throws SQLException {
        Follow follow = null;
        switch(type) {
            case FOLLOW_BRIEF:
                follow = parseHome(new Follow(), rs);
                follow.setHeadimg(rs.getString("headimg"));
                follow.setDesinfo(rs.getString("desinfo"));
                if (rs.getLong("fgid") == 0) {
                    follow.setFgName("特别关注");
                } else if (rs.getLong("fgid") == -1 || rs.getLong("fgid") == Constant.num_n_2) {
                    follow.setFgName("");
                } else {
                    follow.setFgName(rs.getString("fgname"));
                }
//                if (rs.getShort("fstate") == 1) {
//                    follow.setStateStr("密友");
//                } else if (rs.getShort("fstate") == 2){
//                    follow.setStateStr("等待确认中");
//                } else {
//                    follow.setStateStr("");
//                }
                if (follow.getState() == 1) {
                    follow.setFollowStr("已关注");
                } else if(follow.getState() == 2) {
                    follow.setFollowStr("互相关注");
                }
                return follow;
            case FOLLOW_DO:
                follow = parseHome(new Follow(), rs);
                follow.setHeadimg(rs.getString("headimg"));
                follow.setFnickname(rs.getString("fnickname"));
                follow.setFheadimg(rs.getString("fheadimg"));
            case FOLLOW_FRIEND:
                follow = parseHome(new Follow(), rs);
                follow.setHeadimg(rs.getString("headimg"));
                follow.setNickname(rs.getString("nickname"));
                follow.setSex(rs.getString("sex"));  
                follow.setAddress(rs.getString("address"));
                follow.setAge(rs.getInt("age"));
                follow.setAccount(rs.getString("account"));
                if(rs.getInt("ss")==1){
                	follow.setRelation(2);
                }
                if(rs.getInt("sc")==1){
                	follow.setRelation(1);
                }
                if(rs.getInt("st")==2){
                	follow.setRelation(1);
                }
                return follow;
            default:
                return parseHome(new Follow(), rs);
        }
    
    }

    public static Follow parseHome(Follow h, ResultSet rs) throws SQLException {
        return parseHome(h, rs, null);
    }

    public static Follow parseHome(Follow h, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            h.setFoid(rs.getLong(su+"foid"));
        } catch (SQLException e) {
            h.setFoid(null);
        }
        try {
            h.setUid(rs.getLong(su+"uid"));
        } catch (SQLException e) {
            h.setUid(null);
        }
        try {
            h.setFuid(rs.getLong(su+"fuid"));
        } catch (SQLException e) {
            h.setFuid(null);
        }
        try {
            h.setFgid(rs.getLong(su+"fgid"));
        } catch (SQLException e) {
            h.setFgid(null);
        }
        try {
            h.setRemark(rs.getString(su+"remark"));
        } catch (SQLException e) {
            h.setRemark(null);
        }
        try {
            h.setState(rs.getShort(su+"state"));
        } catch (SQLException e) {
            h.setState(null);
        }
        try {
            h.setCdate(rs.getTimestamp(su+"cdate"));
        } catch (SQLException e) {
            h.setCdate(null);
        }
        try {
            h.setNickname(rs.getString(su+"nickname"));
        } catch (SQLException e) {
            h.setNickname(null);
        }
        return h;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, Follow h) {
        par.put("foid",h.getFoid());
        par.put("uid",h.getUid());
        par.put("fuid",h.getFuid());
        par.put("fgid",h.getFgid());
        par.put("remark",h.getRemark());
        par.put("state",h.getState());
        par.put("cdate",h.getCdate());
        par.put("nickname",h.getNickname());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, Follow h) {
        args.put("foid",h.getFoid() );
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final Follow h) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(h),
                        new String[] { "foid" });
                parsePS(ps, h);
                return ps;
            }

        };
    }

    private static String parseSQL(Follow h) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into follow(");
        int count = 0;
        if (null != h.getUid()) {
            sql.append("uid,");
            count++;
        }
        if (null != h.getFuid()) {
            sql.append("fuid,");
            count++;
        }
        if (null != h.getFgid()) {
            sql.append("fgid,");
            count++;
        }
        if (null != h.getRemark()) {
            sql.append("remark,");
            count++;
        }
        if (null != h.getState()) {
            sql.append("state,");
            count++;
        }
        if (null != h.getCdate()) {
            sql.append("cdate,");
            count++;
        }

        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, Follow h)
        throws SQLException {
        int count = 1;
        if (null != h.getUid()) {
            ps.setLong(count++,h.getUid());
        }
        if (null != h.getFuid()) {
            ps.setLong(count++,h.getFuid());
        }
        if (null != h.getFgid()) {
            ps.setLong(count++,h.getFgid());
        }
        if (null != h.getRemark()) {
            ps.setString(count++,h.getRemark());
        }
        if (null != h.getState()) {
            ps.setShort(count++,h.getState());
        }
        if (null != h.getCdate()) {
            ps.setTimestamp(count++,h.getCdate());
        }
    }

}
